// pages/index3/index3.js
const tcity = require("../../utils/citys.js");
const QQMapWX = require('../../libs/qqmap-wx-jssdk.min.js');
let qqmapsdk;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    provinces: [],
    province: "",
    provinceId: "",
    citys: [],
    city: "",
    cityId: "",
    areas: [],
    area: '',
    areaId:'',
    address:'',
   
    tel: wx.getStorageSync("tel"),
    condition: false,
    img1:'../images/image50.png',
    img2:'../images/image48.png',
    num:1,
    ishidden:false,
    address:"",
    typeName:"",
    pid:"",//类型id
    typeId:'',//分类id
    arrname:'',
    index:'',
    index1:'',
    index2: '',
    type: '0',
    latitude:'',
    longitude:'',
    hiddens:false,// 地图选点
    merchant_name:'',//店铺全称
    short_name:'',//店铺简称
    near:'',//附近
    business_licence_no:'',//营业执照号
    phone:'',//商户电话
    leader_name:'',
    leader_id_card:'',
    leader_id_card_image:'',//身份证照片
    business_licence_image:'',//营业执照照片
    lat: '',
    province_id: '',
    city_id: "",
    area_id: '',
    detail_address: '',
    lng: '',
    province_name:'',
    city_name:'',
    area_name:'',
  },
  radioChange: function (e) {
    this.setData({ type: e.detail.value })

  },
  // 名称
  merchant_name(e){
    let that = this;
    that.setData({ merchant_name:e.detail.value})
  },
  short_name(e) {
    let that = this;
    that.setData({ short_name: e.detail.value })
  },
  business_licence_no(e) {
    let that = this;
    that.setData({ business_licence_no: e.detail.value })
  },
  near(e) {
    let that = this;
    that.setData({ near: e.detail.value })
  },
  phone(e) {
    let that = this;
    that.setData({ phone: e.detail.value })
  },
  leader_name(e) {
    console.log(e.detail.value)
    let that = this;
    that.setData({ leader_name: e.detail.value })
  },
  leader_id_card(e) {
    let that = this;
    that.setData({ leader_id_card: e.detail.value })
  },
 
  bindPickerChange: function (e) {
    let that = this;
    that.setData({
      index: e.detail.value,
      province: that.data.provinces[e.detail.value].name,
      provinceId: that.data.provinces[e.detail.value].id,
      ishidden:true
    })
    that.cityList(that.data.provinceId)
  },

  cityChange(e){
    let that = this;
    that.setData({
      index1: e.detail.value,
      city: that.data.citys[e.detail.value].name,
      cityId: that.data.citys[e.detail.value].id,
      ishidden: true
    })
    that.areaList(that.data.cityId)
  }, 
  areaChange(e) {
    let that = this;
    that.setData({
      index2: e.detail.value,
      area: that.data.areas[e.detail.value].name,
      areaId: that.data.areas[e.detail.value].id,
      ishidden: true
    })
  }, 
  gomap1(){
    let that = this;
    that.setData({
      hiddens: false
    })
  },
  gomap:function(e){
    let that = this;
    that.setData({
      hiddens:!that.data.hiddens
    })
    that.moveToLocation();
    // wx.navigateTo({
    //   url: '../map/map'
    // })
  },
  atuoGetLocation(e){
    let that = this;
    that.setData({address: e.detail.value})
    if (!that.data.city){
      wx.showToast({
        title: '请选择省市',
      })
      return;
    }
    var obj = that.data.province + "" + that.data.city + "" + that.data.area + "" + that.data.address;
    console.log(obj)
    qqmapsdk.geocoder({
      address: obj,
      success: function (res) {
        console.log(res);
        that.setData({ latitude: res.result.location.lat, longitude: res.result.location.lng})
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },
  //移动选点
  moveToLocation: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        console.log(res);
        that.setData({ latitude: res.latitude, longitude: res.longitude, address: res.name })
        //选择地点之后返回到原来页面
        // wx.navigateTo({
        //   url: "/pages/index3/index3?address=" + res.name + "&num=2&latitude=" + res.latitude + "&longitude=" + res.longitude
        // });
      },
      fail: function (err) {
        wx.navigateTo({
          url: "/pages/index3/index3"
        })
      }
    });
  },
  preview: function () {
    var that = this
    wx.navigateTo({
      url: '../preview/preview'
    })
  },
  showModal(error) {
    wx.showModal({
      content: error,
      showCancel: false,
    })
  },
  
  formSubmit: function (e) {
    let that = this;
    let form =e.detail.value;
    // console.log(form.label)
    // if (form.label!=0){
     
      var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
      if (!form.merchant_name) {
        that.showModal("请输入店铺全称")
        return;
      }
      if (!form.short_name) {
        that.showModal("请输入店铺简称")
        return;
      }
      if (!that.data.provinceId) {
        that.showModal("请选择所在省")
        return;
      }
      if (!that.data.cityId) {
        that.showModal("请选择市")
        return;
      }
      if (!that.data.areaId) {
        that.showModal("请选择区")
        return;
      }
      if (!that.data.longitude) {
        that.showModal("请在地图标注位置")
        return;
      }
      if (that.data.longitude==0) {
        that.showModal("请在地图标注位置")
        return;
      }
      if (!form.phone) {
        that.showModal("请输入商户电话")
        return;
      }
      if (!form.leader_name) {
        that.showModal("请输入经营者姓名")
        return;
      }
      if (!form.leader_id_card) {
        that.showModal("请输入经营者身份证号")
        return;
      }
      if (!reg.test(form.leader_id_card)) {
        that.showModal("请输入正确身份证号")
        return;
      }
      if (that.data.img1 == "../images/image50.png") {
        that.showModal("请上传身份证照片")
        return;
      }
      if (that.data.img2 == "../images/image48.png") {
        that.showModal("请上传营业执照照片")
        return;
      }
      let postData = {
        merchant_type_id: that.data.typeId,
        province_id: that.data.provinceId,
        city_id: that.data.cityId,
        area_id: that.data.areaId,
        lng: that.data.longitude,
        lat: that.data.latitude,
        leader_id_card_image: that.data.img1,
        business_licence_image: that.data.img2,
        org_type_id: that.data.pid,
        ...form
      }
      app.post("/merchant/active", "put", postData).then(res => {
       wx.showToast({
         title: '提交成功',
       })
        setTimeout(function(){
          wx.reLaunch({
            url: '../index/index'
          })
        },500)
        
      })
    // }else{
    //   let postData = {
    //     merchant_type_id: that.data.typeId,
    //     province_id: that.data.provinceId ? that.data.provinceId:'',
    //     city_id: that.data.cityId ? that.data.cityId:'',
    //     area_id: that.data.areaId ? that.data.areaId:'',
    //     lng: that.data.longitude ? that.data.longitude:"",
    //     lat: that.data.latitude ? that.data.latitude:'',
    //     leader_id_card_image: that.data.img1,
    //     business_licence_image: that.data.img2,
    //     is_active: 0,
    //     org_type_id: that.data.pid,
    //     ...form
    //   }
    //   app.post("/merchant/active", "put", postData).then(res => {
    //     wx.showToast({
    //       title: '保存成功',
    //     })
    //     setTimeout(function () {
    //       wx.reLaunch({
    //         url: '../index/index'
    //       })
    //     }, 500)
    //   })
    // }
    
    
    
   
  },
	// save2: function () {
  //   wx.reLaunch({
  //     url: '../indexActive/indexActive'
  //   })
  // },
   

  //上传照片
  clickImg:function(e){
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://api.51huijingang.com/api/merchant/upload', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: "file",
          success: function (data) {
            var datas= JSON.parse(data.data);
            console.log(datas)
            var datass = datas.url
            that.setData({ img1: datass })
          }
        })
      },
    })
  },
  //上传营业执照
  clickImg1: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://api.51huijingang.com/api/merchant/upload', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: "file",
          success: function (data) {
            var datas = JSON.parse(data.data);
            console.log(datas)
            var datass = datas.url
            that.setData({ img2: datass })
          }
        })
      }
    })
      
    
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    qqmapsdk = new QQMapWX({
      key: 'FOCBZ-7ZM3I-EMQGT-5CLSU-WICKT-OXBK2' //这里自己的key秘钥进行填充
    });
    that.setData({
      typeName:e.TypeName,
      pid: e.pid,//类型id
      typeId: e.TypeId,//分类id
      arrname: e.arrname,
    });
    //信息
    app.post("/index/merchant_info","get",{}).then(res =>{
      console.log(res)
      that.setData({
        merchant_name: res.merchant.merchant_name,
        short_name: res.organization.short_name,
        business_licence_no: res.organization.business_licence_no,
        provinceId: res.merchant.province_id,
        cityId: res.merchant.city_id,
        areaId: res.merchant.area_id,
        detail_address: res.merchant.detail_address,
        near: res.merchant.near,
        longitude: res.merchant.lng,
        latitude: res.merchant.lat,
        phone: res.merchant.phone,
        leader_name: res.organization.leader_name,
        leader_id_card: res.organization.leader_id_card ,
        img1: res.organization.leader_id_card_image ? res.organization.leader_id_card_image : '../images/image50.png',
        img2: res.organization.business_licence_image ? res.organization.leader_id_card_image : '../images/image48.png',
        province_name: res.merchant.province_name,
        city_name: res.merchant.city_name,
        area_name: res.merchant.area_name,
        address: res.merchant.lng?"已标注":'',
      })
    })
    if (e.address != null && e.address != '' && e.num != null && e.num != '' && e.latitude != null && e.latitude != '' && e.longitude != null && e.longitude != '') {
      //设置变量 address 的值
      that.setData({
        address: e.address,
        num: e.num,
        latitude: e.latitude,
        longitude: e.longitude,
      });
    };
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that  = this;
    var tel = wx.getStorageSync("tel");
    console.log(tel);
    // 省
    app.post("/region/list","get",{}).then(res =>{
      console.log(res)
      that.setData({ provinces:res})
    })
  },
  cityList(id){
    let that = this;
    app.post("/region/list", "get", {pid:id}).then(res => {
      console.log(res)
      that.setData({ citys: res })
    })
  },
  areaList(id){
    let that = this;
    app.post("/region/list", "get", { pid: id }).then(res => {
      console.log(res)
      that.setData({ areas: res })
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})